<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$query = "select `C`.`company_id`,`C`.`name`
		from `companies` as `C`,`agent` as `A`
		where
			`C`.`company_id` = `A`.`company_id` and
			`A`.`user_id` = '".$user->getUserId()."'
		limit 1";
$company = $db->getRow($query);

$query = "select
			`vote_id`,`nickname`,`text`,`mark`,`time`,`comment`
		from
			`votes`
		where
			`object_type` = 'company' and
			`object_id` = '{$company['company_id']}'".
		($route->getAdminPlace() == "new" ? " and `comment` = ''" : ($route->getAdminPlace() == "old" ? " and `comment` != ''" : ""))
		."
		order by `time` desc";
$votes = $db->getTable($query);

loadPage("head.php");
?>
<script type="text/javascript">
function cancel_comment(id){
	$("#edit_comment_"+id).hide();
	$("#comment_"+id).show();
}
function edit_comment(id){
	$("#comment_"+id).hide();
	$("#edit_comment_"+id).show()
		.find("textarea").focus();
}
function post_comment(id){
	var text = $("#edit_comment_"+id+" textarea").val();
	$.ajax({
		url: "/agent/post",
		type: "post",
		data: "id="+id+"&text="+encodeURIComponent(text),
		success: function(data){
			var response = $.parseJSON(data);
			if (response.type == "success"){
				$("#comment_"+id).empty().append(response.text);
				cancel_comment(id);
				$("#edit_comment_"+id+" textarea").val(response.text2.replace(/\\r/g,"\r").replace(/\\n/g,"\n"));
			} else if (response.type == "error") {
				alert("Возникла ошибка. Пожалуйста, попробуйте позже.");
			} else if (response.type == "access") {
				alert("У вас нет права редактировать эту запись.");
			} else if (response.type == "empty") {
				alert("Необходимо заполнить ответ.");
			}
		}
	});
}
</script>
<br /><h1>Отзывы про «<?php echo $company['name']; ?>»</h1><br />
<a href="/agent"<?php if ($route->getAdminPlace() == "index") echo "style='font-weight:bold'"; ?>>Все</a> |
<a href="/agent/new"<?php if ($route->getAdminPlace() == "new") echo "style='font-weight:bold'"; ?>>Новые</a> |
<a href="/agent/old"<?php if ($route->getAdminPlace() == "old") echo "style='font-weight:bold'"; ?>>С комментарием</a><br /><br />
<?php
if ($votes){
	?>
	<table cellpadding='0' cellspacing='0' class='list agent_list'>
		<tr class='table_header'>
			<td width='50%'>Отзыв</td>
			<td width='50%' class='actions'>Комментарий</td>
		</tr><?php
		foreach ($votes as $vote){
			echo "<tr>";
				echo "<td>";
					echo "<span class='bold'>Пользователь:</span> $vote[nickname]<br />";
					echo "<span class='bold'>Время:</span> ".date("d.m.Y H:i",$vote['time'])."<br />";
					echo "<span class='bold'>Оценка:</span> ".($vote['mark'] == 1 ?
							"<span style='color:green'>Положительно</span>" : ($vote['mark'] == 0 ?
							"<span style='color:gray'>Нейтриально</span>" :
							"<span style='color:red'>Отрицательно</span>"));
					echo "<br /><span class='bold'>Текст:</span> $vote[text]";
				echo "</td>";
				echo "<td>";
					echo "<div id='comment_{$vote['vote_id']}' class='agent_comment' title='ответить' onclick='edit_comment({$vote['vote_id']})'>";
					if ($vote['comment'] != ""){
						echo str_replace("\n","",nl2br($vote['comment']));
					} else {
						echo "<span class='to_comment' style=''>Комментировать...</span>";
					}
					echo "</div>";
					?>
					<div class='edit_comment' id='edit_comment_<?php echo $vote['vote_id']; ?>'>
						<textarea><?php echo $vote['comment']; ?></textarea><br />
						<input type="button" value="Отменить" onclick="cancel_comment(<?php echo $vote['vote_id']; ?>)" />
						<input type="button" value="Сохранить" onclick="post_comment(<?php echo $vote['vote_id']; ?>)" />
					</div><?php
				echo "</td>";
			echo "</tr>";
		}
	?>
	</table>
	<?php
	} else {
		echo "<h3 class='gag'>Нет отзывов</h3>";
}

loadPage("footer.php"); ?>